<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="beforeOpen"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDel">
      <template #menuLeft>
        <ElButton
          v-if="permissions.personnel_resume_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="exportExcel">
          导出
        </ElButton>
        <ElButton
          v-if="permissions.personnel_resume_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-upload"
          @click="$refs.excelUpload.show()">
          导入
        </ElButton>
      </template>
      <template #userNameForm>
        <ElInput v-model="form.userName" placeholder="用户名称" @focus="showSelectUser" />
      </template>
    </FyCurd>
    <!--excel 模板导入 -->
    <ExcelUpload
      ref="excelUpload"
      title="个人简历信息导入"
      :url="`${services.personnelService}resume/import`"
      :temp-url="`${services.personnelService}sys-file/local/resume.xlsx`"
      temp-name="个人简历信息导入模板.xlsx"
      @refreshDataList="refreshChange" />
    <UserPicker
      v-if="visibleSelectUser"
      :reserve-position-on-close="false"
      close-on-click-modal
      :visible.sync="visibleSelectUser"
      @confirm="getUserData" />
  </BasicContainer>
</template>

<script>
  import { addObj, delObj, fetchList, putObj } from '@/api/personnel/resume'
  import { tableOption } from '@/const/crud/personnel/resume'

  import { services } from '@/config'
  import { mapState } from 'vuex'

  export default {
    name: 'Fypersonnelresume',

    props: {
      userName: {
        type: String
      }
    },
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        form: {},
        userInfoId: 0,
        visibleSelectUser: false,
        services
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.personnel_resume_add, false),
          delBtn: this.$isEmpty(this.permissions.personnel_resume_del, false),
          editBtn: this.$isEmpty(this.permissions.personnel_resume_edit, false)
        }
      }
    },
    methods: {
      // 给userinfo组件来初始化
      userInit(userInfoId) {
        this.userInfoId = userInfoId
        this.form.userName = this.userName
        this.searchForm.userinfoId = userInfoId
        this.getList(this.page)
      },
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 删除
      handleDel: function (row, index) {
        if (this.userInfoId !== -1) {
          this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              return delObj(row.id)
            })
            .then(data => {
              this.$message.success('删除成功')
              this.getList(this.page)
            })
        } else {
          for (let i = index; i < this.tableData.length - 1; i++) {
            this.tableData[i] = this.tableData[i + 1]
          }
          this.tableData.pop()
          this.$message.success('删除成功')
        }
      },
      // 更新
      handleUpdate: function (row, index, done, loading) {
        if (this.userInfoId !== -1) {
          putObj(row)
            .then(data => {
              this.$message.success('修改成功')
              done()
              this.getList(this.page)
            })
            .catch(() => {
              loading()
            })
        } else {
          for (let i = index; i < this.tableData.length - 1; i++) {
            this.tableData[i] = this.tableData[i + 1]
          }
          this.tableData.pop()
          this.tableData.push(row)
          this.$message.success('修改成功')
          done()
        }
      },
      // 保存
      handleSave: function (row, done, loading) {
        if (this.userInfoId !== -1) {
          addObj(row)
            .then(data => {
              this.$message.success('添加成功')
              done()
              this.getList(this.page)
            })
            .catch(() => {
              loading()
            })
        } else {
          row.userName = this.userName
          this.tableData.push(row)
          done()
        }
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        if (this.userInfoId !== 0 || this.userInfoId !== -1) {
          this.searchForm.userinfoId = this.userInfoId
        }
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        if (this.userInfoId !== -1) {
          this.getList(this.page)
        }
      },
      //  导出excel
      exportExcel() {
        this.$downBlobCustomeFile(
          `${services.personnelService}resume/export`,
          this.searchForm,
          this.tableOption.column,
          `个人简历信息${this.$dateFormat(new Date())}.xlsx`
        )
      },
      beforeOpen(done, type) {
        if (type === 'add') {
          if (this.userInfoId === 0) {
            // getId().then(response => {
            //   this.form.userinfoId = response.data.data
            // })
          } else {
            this.form.userName = this.userName
            this.form.userinfoId = this.userInfoId
            this.tableOption.column[0].display = false
          }
        }
        done()
      },
      groupSave(userInfoId) {
        for (let i = 0; i < this.tableData.length; i++) {
          this.tableData[i].userinfoId = userInfoId
          addObj(this.tableData[i])
        }
      },
      getUserData(userData) {
        const user = userData[0]
        this.form.userinfoId = user.userInfoId // 用户id
        this.form.userName = user.name // 用户名称
        this.form.unitName = user.deptName // 单位名称
      },
      showSelectUser() {
        this.visibleSelectUser = true
      }
    }
  }
</script>
